//
//  RCPointsShopViewController.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-11-21.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCPointsShopViewController.h"
#import "RCPointsShopListDataModel.h"
#import "IManager+ModulePointsShopList.h"
#import "RCPointsShopListCell.h"
#import "MBProgressHUD.h"
#import "NSArray+EX.h"
#import "RCChangePointDetailViewController.h"

@interface RCPointsShopViewController ()<IManagerDelegate>

@property(nonatomic,retain)MBProgressHUDEx *progressHub;
@property(nonatomic,assign)BOOL refresh;
@property(nonatomic,assign)NSInteger  pn;
@property(nonatomic,assign)NSInteger  total;
@property(nonatomic,retain)RCPointsShopListDataModel *dataModel;
@property(nonatomic,assign)int64_t requestID;
@property(nonatomic,retain)NSMutableArray *dataArray;

@end

@implementation RCPointsShopViewController

@synthesize progressHub = _progressHub;
@synthesize refresh = _refresh;
@synthesize pn = _pn;
@synthesize total = _total;
@synthesize dataModel = _dataModel;
@synthesize requestID = _requestID;
@synthesize dataArray = _dataArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)init{
    self = [super init];
    if (self) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return self;
}

-(void)loadView{
    [super loadView];
    self.titleLabelText = @"积分商城";
    self.navigationItem.titleView = [self createTitleView];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_requestID];
}

-(void)dealloc{
    
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_requestID];
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    self.dataArray = nil;
    self.dataArray = nil;
    [super dealloc];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    if ( baseVCTableView != nil ) {
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        //        baseVCTableView.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.backgroundColor = [UIColor clearColor];
        baseVCTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        baseVCTableView.frame = self.view.bounds;
        CGRect tableViewNewFrame  = CGRectMake(baseVCTableView.frame.origin.x,baseVCTableView.frame.origin.y,baseVCTableView.frame.size.width,UIScreenHeight-20-self.navigationController.navigationBar.frame.size.height-4);
        
        baseVCTableView.frame = tableViewNewFrame;
        baseVCTableView.tableHeaderView = nil;
        baseVCTableView.tableFooterView = nil;
        [self addPullRefresh];
    }
    [self refreshData];

}

#pragma mark TableView Refresh And LoadMore
- (void) refreshData
{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _refresh = YES;
    _pn = 0;
    
    _requestID = [[IManager sharedInstance]requestPointsShopListInfo:0 withRn:10 withDelegate:self];
    
    
}



- (void) loadMoreData
{
    _refresh = NO;
    
    //    NSString *page = [NSString stringWithFormat:@"%d",_pn];
    
    _requestID = [[IManager sharedInstance]requestPointsShopListInfo:_pn withRn:10 withDelegate:self];

}

#pragma mark UITableView Datasource & Delegate

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (nil == _dataArray) {
        return 0;
    }
    return [_dataArray count];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [RCPointsShopListCell getCellHeight];
}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"RCPointShopCellIdentifier";
    RCPointsShopListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (nil == cell) {
        cell = [[[RCPointsShopListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
    }
    RCPointsShopListItem *item = [_dataArray safeObjectAtIndex:[indexPath row]];
    if (nil == item) {
        return nil;
    }
    [cell bindData:item];
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    RCPointsShopListItem *item = [_dataArray safeObjectAtIndex:[indexPath row]];
    if (nil == item) {
        return;
    }
    RCChangePointDetailViewController *detailVC = [[RCChangePointDetailViewController alloc]initWithGoodsID:item.itemID];
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}


#pragma mark Imanager Delegate


- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        if (_refresh) {
            [_progressHub removeFromSuperview];
            [_progressHub release];
            _progressHub = nil;
            [self finishRefreshingData];
            _refresh = NO;
        }else{
            [self finishLoadMoreData];
        }
    }
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    
    if (_requestID == requestId) {
        RCPointsShopListDataModel *resultList = (RCPointsShopListDataModel *)dataModel;
        if (_refresh) {
            [_progressHub removeFromSuperview];
            [_progressHub release];
            _progressHub = nil;
            self.dataModel = resultList;
            self.total = resultList.total;
            [_dataArray removeAllObjects];
            [_dataArray addObjectsFromArray:resultList.itemListArray];
            [self finishRefreshingData];
            _refresh = NO;
//            [baseVCTableView reloadData];
        }else{
            
            self.dataModel = resultList;
            self.total = resultList.total;
            [_dataArray addObjectsFromArray:resultList.itemListArray];
            
            [baseVCTableView reloadData];
            [self finishLoadMoreData];
        }
        
        
        
        NSInteger currentTotal = [_dataArray count];
        _pn = currentTotal;
        
        if (currentTotal < self.total) {
            [self addPullLoadMore];
        }else{
            [self noMoreData];
        }
        
        [baseVCTableView reloadData];
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
